/* Copyright (C) 2010-2011, Mamadou Diop.
*  Copyright (C) 2011, Doubango Telecom.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango(dot)org>
*	
* This file is part of imsdroid Project (http://code.google.com/p/imsdroid)
*
* imsdroid is free software: you can redistribute it and/or modify it under the terms of 
* the GNU General Public License as published by the Free Software Foundation, either version 3 
* of the License, or (at your option) any later version.
*	
* imsdroid is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
* See the GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License along 
* with this program; if not, write to the Free Software Foundation, Inc., 
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package org.doubango.FrCall.Screens;

import org.doubango.FrCall.R;
import org.doubango.ngn.services.INgnConfigurationService;
import org.doubango.ngn.utils.NgnConfigurationEntry;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * @author lihu
 *	简化的设置界面，仅用设置IP和网络类型
 */
public class ScreenSimpleSetting extends BaseScreen {
	private final static String TAG = ScreenSimpleSetting.class.getCanonicalName();
	
	private final INgnConfigurationService mConfigurationService;
	
	private EditText mEtProxyHost;
	private CheckBox mCbSigComp;
	private CheckBox mCbWiFi;
	private CheckBox mCb3G;
	private RadioButton mRbIPv4;
	private RadioButton mRbIPv6;
	private Button setCodesBtn,setNattBtn;
	
	public ScreenSimpleSetting() {
		super(SCREEN_TYPE.SIMPLE_SETTINGS_T, TAG);
		
		this.mConfigurationService = getEngine().getConfigurationService();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_simple_setting);
        
        mEtProxyHost = (EditText)findViewById(R.id.screen_simple_setting_editText_pcscf_host);
        mCbSigComp = (CheckBox)findViewById(R.id.screen_simple_setting_checkBox_sigcomp);
        mCbWiFi = (CheckBox)findViewById(R.id.screen_simple_setting_checkBox_wifi);
        mCb3G = (CheckBox)findViewById(R.id.screen_simple_setting_checkBox_3g);
        mRbIPv4 = (RadioButton)findViewById(R.id.screen_simple_setting_radioButton_ipv4);
        mRbIPv6 = (RadioButton)findViewById(R.id.screen_simple_setting_radioButton_ipv6);
        
        setCodesBtn = (Button) findViewById(R.id.screen_simple_setting_codes_btn);
        setCodesBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent1 = new Intent(ScreenSimpleSetting.this,
						ScreenCodecs.class);
				startActivityForResult(intent1,0);
			}
		});
        setNattBtn = (Button) findViewById(R.id.screen_simple_setting_natt_btn);
        setNattBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent1 = new Intent(ScreenSimpleSetting.this,
						ScreenNatt.class);
				startActivityForResult(intent1,0);
			}
		});
        mEtProxyHost.setText(mConfigurationService.getString(NgnConfigurationEntry.NETWORK_PCSCF_HOST, NgnConfigurationEntry.DEFAULT_NETWORK_PCSCF_HOST));
        mCbSigComp.setChecked(mConfigurationService.getBoolean(NgnConfigurationEntry.NETWORK_USE_SIGCOMP, NgnConfigurationEntry.DEFAULT_NETWORK_USE_SIGCOMP));
        
        mCbWiFi.setChecked(mConfigurationService.getBoolean(NgnConfigurationEntry.NETWORK_USE_WIFI, NgnConfigurationEntry.DEFAULT_NETWORK_USE_WIFI));
        mCb3G.setChecked(mConfigurationService.getBoolean(NgnConfigurationEntry.NETWORK_USE_3G, NgnConfigurationEntry.DEFAULT_NETWORK_USE_3G));
        mRbIPv4.setChecked(mConfigurationService.getString(NgnConfigurationEntry.NETWORK_IP_VERSION,
        		NgnConfigurationEntry.DEFAULT_NETWORK_IP_VERSION).equalsIgnoreCase("ipv4"));
        mRbIPv6.setChecked(!mRbIPv4.isChecked());
        
        // add listeners (for the configuration)
        super.addConfigurationListener(mEtProxyHost);
        super.addConfigurationListener(mCbSigComp);
        super.addConfigurationListener(mCbWiFi);
        super.addConfigurationListener(mCb3G);
        super.addConfigurationListener(mRbIPv4);
        super.addConfigurationListener(mRbIPv6);
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
	
	protected void onPause() {
		if(super.mComputeConfiguration){
			
			mConfigurationService.putString(NgnConfigurationEntry.NETWORK_PCSCF_HOST, //设置服务器IP
					mEtProxyHost.getText().toString().trim());
			mConfigurationService.putBoolean(NgnConfigurationEntry.NETWORK_USE_SIGCOMP,  mCbSigComp.isChecked());
			//设置连接类型，wifi or 3G
			mConfigurationService.putBoolean(NgnConfigurationEntry.NETWORK_USE_WIFI, 
					mCbWiFi.isChecked());
			mConfigurationService.putBoolean(NgnConfigurationEntry.NETWORK_USE_3G, 
					mCb3G.isChecked());
			mConfigurationService.putString(NgnConfigurationEntry.NETWORK_IP_VERSION, //设置IP类型
					mRbIPv4.isChecked() ? "ipv4" : "ipv6");
			
			// Compute
			if(!mConfigurationService.commit()){//存储到本地
				Log.e(TAG, "Failed to commit() configuration");
			}
			
			super.mComputeConfiguration = false;
		}
		super.onPause();
	}
}
